import cv2
import numpy as np


file_images = "C:\\Users\\14197\\Desktop\\program_donqi\\data\\0006\\0006.jpg"
#file_images = "C:\\Users\\14197\\Desktop\\img\\LH200155PDF_30584130_Page10.jpg"
img_origin = cv2.imread(file_images)
cv2.imshow("img_origin", img_origin)





size = img_origin.shape
print(size)
y = size[0]  # 图片垂直尺寸高y
x = size[1]  # 图片水平尺寸宽x

# 一、初始化
image_gauss = cv2.GaussianBlur(img_origin, (3, 3), 0)  # Gauss滤波
img_gray = cv2.cvtColor(image_gauss, cv2.COLOR_BGR2GRAY)  # 转化为灰度图
# cv2.imshow("img_gray", img_gray)

ret, binary = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)  # 图像二值化
cv2.imshow("binary", binary)

# 二、轮廓提取
contours, heriachy = cv2.findContours(~binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
area_max = 0
for i, contour in enumerate(contours):  # 寻找最大轮廓
    cv2.drawContours(img_origin, contours, i, (0, 255, 0), 2)
    # print(i)
    area = cv2.contourArea(contour)  # 用来计算轮廓的面积
    if area > area_max:
        area_max = area
        index = i
cv2.drawContours(img_origin, contours, index, (0, 0, 255), 2)
# print(index)
# print (contours[index])

# 寻找四边形
approxCurve = cv2.approxPolyDP(contours[index], 4, True)  # 用多边形包围轮廓，可以得到严格的矩形，有助于找到角点
# if approxCurve.shape[0] == 4:
#     cv2.drawContours(img_origin, contours, index, (0, 255, 0), 2)
print(approxCurve)

points = approxCurve.flatten()  # 将矩阵变为一维列表
print(points)
print(int((len(points) + 1) / 2))
d0, d1, d2, d3 = [], [], [], []
for i in range(int((len(points) + 1) / 2)):
    d0.append((points[2 * i]) ** 2 + (points[2 * i + 1]) ** 2)
    d1.append((points[2 * i]-x) ** 2 + (points[2 * i + 1]) ** 2)
    d2.append((points[2 * i]-x) ** 2 + (points[2 * i + 1]-y) ** 2)
    d3.append((points[2 * i]) ** 2 + (points[2 * i + 1]-y) ** 2)

p0 = (points[2*d0.index(min(d0))], points[2*d0.index(min(d0))+1] )
p1 = (points[2*d1.index(min(d1))], points[2*d1.index(min(d1))+1] )
p2 = (points[2*d2.index(min(d2))], points[2*d2.index(min(d2))+1] )
p3 = (points[2*d3.index(min(d3))], points[2*d3.index(min(d3))+1] )





'''
max_index = np.argmax(approxCurve, axis=0)
min_index = np.argmin(approxCurve, axis=0)

print(max_index[0][0])
# print(approxCurve[3][0])
# print (contours[index][x_max_index])
# (p1_x, p1_y) = (approxCurve[(max_index[0])][0],approxCurve[max_index][1])
corner1 = (points[(2 * max_index[0][0])], points[(2 * max_index[0][0] + 1)])
corner2 = (points[(2 * max_index[0][1])], points[(2 * max_index[0][1] + 1)])
corner3 = (points[(2 * min_index[0][0])], points[(2 * min_index[0][0] + 1)])
corner4 = (points[(2 * min_index[0][1])], points[(2 * min_index[0][1] + 1)])

print(corner1, corner2, corner3, corner4)
'''
print(p0,p1,p2,p3)
cv2.circle(img_origin, p0, 10, (255, 0, 0), 2)
cv2.circle(img_origin, p1, 10, (255, 0, 0), 2)
cv2.circle(img_origin, p2, 10, (255, 0, 0), 2)
cv2.circle(img_origin, p3, 10, (255, 0, 0), 2)

'''
hull = cv2.convexHull(contours[index], False)  #寻找凸包
print(hull)
length =  len(hull)
for i in range(len(hull)):
    cv2.circle(img_origin, tuple(hull[i][0]), 2, (255, 0, 0), 2)
    #line(img_origin, tuple(hull[i][0]), tuple(hull[(i + 1) % length][0]), (0, 255, 0), 2)

print(tuple(hull[i][0]))
print(hull[i][0])
'''
# max_area = np.argmax(area)# 返回array最大值位置
# img_origin = cv2.resize(img_origin, (int(x / 2), int(y / 2)))
cv2.imshow("binary2", img_origin)

'''
#中央涂白
cv2.rectangle(img_gray, (int(a/10), int(b/5)), (int(9*a/10), int(4*b/5)), (255, 255, 255), -1)
img_gray = cv2.resize(img_gray, (int(a/ 2), int(b/ 2)))
cv2.imshow("musk", img_gray)
'''

cv2.waitKey(0)
cv2.destroyAllWindows()



